package emailutil

import (
	"gopkg.in/gomail.v2"
	"github.com/sirupsen/logrus"
)

/**
 * @Description: 发送邮件
 * @param email 邮件
 * @param seats 空闲座位号
 * @param autoGrab 是否开启自动抢座
 */
func SendEmail(email string, seats []string, autoGrab bool) {
	dialer := gomail.NewDialer("smtp.qq.com", 465, "820863776@qq.com", "rmibfclqlfuibcfb")
	message := gomail.NewMessage()
	message.SetAddressHeader("From", "820863776@qq.com", "图书馆抢座系统提醒助手")
	message.SetHeader("To", email)
	message.SetHeader("Subject", "图书馆抢座系统提醒")
	var str string
	if autoGrab {
		str = "已成功帮您抢到座位" + seats[0] + "号的位置，请在30分钟内到图书馆签到哦～"
	} else {
		str = "您所感兴趣的区域有空闲的座位啦，快去预约叭!\n"
		for _, seat := range seats {
			str += seat + "\n"
		}
	}
	message.SetBody("text/plain", str)
	if err := dialer.DialAndSend(message); err != nil {
		logrus.Error(err.Error())
	}
}
